<template>
  <van-picker
    title="选择性别"
    show-toolbar
    :columns="columns"
    @confirm="onConfirm"
    @cancel="onCancel"
    :default-index="defaultIndex"
  />
</template>
<script>
import { updateUserInfo } from "@/api/User";
export default {
  props: {
    value: {
      type: [String, Number],
      default: 0,
    },
  },
  data() {
    return {
      columns: ["男", "女"],
      defaultIndex: this.value,
    };
  },
  methods: {
    async onConfirm(value, index) {
      // value就是当前选中的值； index是选中值的索引
      console.log(`当前值：${value}, 当前索引：${index}`);
      try {
        await updateUserInfo({
          gender: index, // 0 或 1
        });
        this.$emit("input", index);
        this.$emit("close");
      } catch (error) {
        console.log(error);
        this.$toast.fail("更新失败");
      }
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style></style>
